package com.sywb.chuangyebao.contract;

import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.sywb.chuangyebao.R;
import com.sywb.chuangyebao.utils.HttpCallback;
import com.sywb.chuangyebao.utils.HttpUtils;

import org.bining.footstone.adapter.BaseRecyclerAdapter;
import org.bining.footstone.adapter.ViewHolderHelper;
import org.bining.footstone.log.Logger;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public interface ContactUsContract {

    interface View extends BaseRecyclerContract.View {
        void setTvIntro(String intro);
    }

    class Presenter extends BaseRecyclerContract.Presenter<View> {

        private ContactUsAdapter recyclerAdapter;

        @Override
        public void onStart() {
            super.onStart();
            recyclerAdapter = new ContactUsAdapter(mContext);
            setRecyclerAdapter(recyclerAdapter, false);
            initData();
        }

        @Override
        public void initData() {
            super.initData();
            aboutContactUs();
        }

        @Override
        public void onRecyclerItemClick(int postion) {
        }

        @Override
        public boolean useRecyclerDivider() {
            return true;
        }


        public void aboutContactUs() {
            HttpUtils.aboutContactUs(new HttpCallback<String>() {
                @Override
                public void onSuccess(String contactus) {
                    HashMap<String, String> contactusMap = JSON.parseObject(contactus, new TypeReference<HashMap<String, String>>() {
                    });
                    if (contactusMap != null && contactusMap.size() > 0) {
                        List<ContactUsInfo> contactUsInfos = new ArrayList<>();
//                        for (Map.Entry<String, String> item : contactusMap.entrySet()) {
//                            ContactUsInfo contactUsInfo = new ContactUsInfo();
//                            contactUsInfo.key = item.getKey();
//                            contactUsInfo.value = item.getValue();
//                            if (!contactUsInfo.key.equals("des")) {
//                                contactUsInfos.add(contactUsInfo);
//                            }
//                        }

                        if (contactusMap.containsKey("contact")) {
                            String contactJson = contactusMap.get("contact");
                            List<String> contact = JSON.parseArray(contactJson, String.class);
                            for (String itemjson : contact) {
                                Logger.e("item:" + itemjson);

                                List<String> item = JSON.parseArray(itemjson, String.class);

                                ContactUsInfo contactUsInfo = new ContactUsInfo();
                                contactUsInfo.key = item.get(0);
                                contactUsInfo.value = item.get(1);
                                contactUsInfos.add(contactUsInfo);
                            }
//                            ContactUsInfo contactUsInfo = new ContactUsInfo();
//                            contactUsInfo.key = "官方微信";
//                            contactUsInfo.value = contactusMap.get("weixin");
//                            contactUsInfos.add(contactUsInfo);
                        }

//                        if (contactusMap.containsKey("weixin")) {
//                            ContactUsInfo contactUsInfo = new ContactUsInfo();
//                            contactUsInfo.key = "官方微信";
//                            contactUsInfo.value = contactusMap.get("weixin");
//                            contactUsInfos.add(contactUsInfo);
//                        }
//                        if (contactusMap.containsKey("qq")) {
//                            ContactUsInfo weixin = new ContactUsInfo();
//                            weixin.key = "客服QQ";
//                            weixin.value = contactusMap.get("qq");
//                            contactUsInfos.add(weixin);
//                        }
//                        if (contactusMap.containsKey("email")) {
//                            ContactUsInfo weixin = new ContactUsInfo();
//                            weixin.key = "联系邮箱";
//                            weixin.value = contactusMap.get("email");
//                            contactUsInfos.add(weixin);
//                        }

                        if (contactusMap.containsKey("des") && mView != null) {
                            mView.setTvIntro(contactusMap.get("des"));
                        }
                        recyclerAdapter.setDatas(contactUsInfos);
                    }
                }

                @Override
                public void onError(String strMsg) {
                    super.onError(strMsg);
                    showMessage(strMsg);
                }

                @Override
                public void onFinish() {
                    super.onFinish();
                    onFinishAsync();
                    isNoData();
                }
            });
        }

        class ContactUsAdapter extends BaseRecyclerAdapter<ContactUsInfo> {

            public ContactUsAdapter(Context context) {
                super(context, R.layout.item_contact_us);
            }

            @Override
            protected void setItemData(ViewHolderHelper viewHolderHelper, int i, ContactUsInfo model) {
                viewHolderHelper.setText(R.id.tv_contact_key, model.key);
                viewHolderHelper.setText(R.id.tv_contact_value, model.value);
            }
        }

        class ContactUsInfo {

            public String key;
            public String value;
        }
    }

}

